package com.vimi8.chick.plugins.module;

import android.app.Activity;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.weex.plugin.annotation.WeexModule;
import com.eros.framework.manager.ManagerFactory;
import com.eros.framework.utils.JsPoster;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.vimi8.chick.plugins.manager.ShareManager;
import com.vimi8.chick.plugins.model.ShareBean;
import com.vimi8.chick.plugins.model.ShareInitBean;
import com.vimi8.chick.plugins.utils.ShareUtil;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@WeexModule(name = "vmShare", lazyLoad = true)
public class ShareModule extends WXModule {

    @JSMethod(uiThread = true)
    public void initShare(String params) {
        try {
            ShareInitBean bean = JSON.parseObject(params, ShareInitBean.class);
            if (bean == null || TextUtils.isEmpty(bean.getUmeng())) {
                return;
            }
            ShareManager shareManager = ManagerFactory.getManagerService(ShareManager.class);
            shareManager.initShare(bean, mWXSDKInstance.getContext());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JSMethod(uiThread = true)
    public void shareText(String params, JSCallback jsCallback) {
        try {
            ShareBean bean = JSON.parseObject(params, ShareBean.class);
            Activity activity = (Activity) mWXSDKInstance.getContext();
            ShareUtil.INSTANCE.shareText(activity,
                bean.getTitle(), bean.getImage(),
                new JsShareListener(activity, jsCallback));
        } catch (Exception e) {
            e.printStackTrace();
            JsPoster.postFailed("分享失败", jsCallback);
        }
    }

    @JSMethod(uiThread = true)
    public void shareUrl(String params, JSCallback jsCallback) {
        try {
            ShareBean bean = JSON.parseObject(params, ShareBean.class);
            Activity activity = (Activity) mWXSDKInstance.getContext();
            ShareUtil.INSTANCE.shareUrl(activity,
                bean.getTitle(), bean.getUrl(), bean.getDescription(), bean.getImage(),
                new JsShareListener(activity, jsCallback));
        } catch (Exception e) {
            e.printStackTrace();
            JsPoster.postFailed("分享失败", jsCallback);
        }

    }

    class JsShareListener extends ShareUtil.UMShareListenerImpl {
        JSCallback jsCallback;
        Activity activity;

        public JsShareListener(@NotNull Activity activity, JSCallback jsCallback) {
            super(activity);
            this.activity = activity;
            this.jsCallback = jsCallback;
        }

        @Override
        public void onResult(@Nullable SHARE_MEDIA p0) {
            super.onResult(p0);
            JsPoster.postSuccess("分享成功", jsCallback);
        }

        @Override
        public void onError(@Nullable SHARE_MEDIA p0, @Nullable Throwable p1) {
            super.onError(p0, p1);
            JsPoster.postFailed("分享失败", jsCallback);
        }

        @Override
        public void onCancel(@Nullable SHARE_MEDIA p0) {
            super.onCancel(p0);
            JsPoster.postFailed("取消分享", jsCallback);
        }
    }
}
